﻿
using System;
using System.Threading;
using System.Windows;
using Waver_Inter;
using Waver_Inter.Entity;
using Waver_Inter.Exception;

namespace WpfAppTimes
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private uint Sn = 3729837756;
        public MainWindow()
        {
            InitializeComponent();
            Init_Event();
            this.Closed += close;
        }
        //wz比赛架包的事件监听
        private void Init_Event()
        {
            WZ_Link_Inter.Instance.SdkErrorEvent += Error;
            WZ_Link_Inter.Instance.OnBulckInitialCompleteEvent += BulckInitialComplete;
            WZ_Link_Inter.Instance.OnVarChanged += VarChanged;
            //订阅字符串改变返回事件(RFID内容)
            WZ_Link_Inter.Instance.OnStrChanged += StrChanged;
            WZ_Link_Inter.Instance.OnDevChanged += DevChanged;
        }
        //取消所有关注
        private void close(object sender, EventArgs e)
        {
            WZ_Link_Inter.Instance.Cancel_AATT_ALL(Sn);
        }
        //错误的消息提示内容
        private void Error(MessageType arg1, SDKException arg2)
        {
            MessageBox.Show(arg2.Message);
        }

        private void BulckInitialComplete(string obj)
        {
            
            App.Current.Dispatcher.Invoke((Action)(() => {
                lianJie.Content = "登陆成功！";
                if (!string.IsNullOrEmpty(obj))
                {
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30008, 1);//关注变量
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30008, 2);//关注变量
                    //yanChi();
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30009, 1);//关注变量
                    //yanChi();
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30010, 1);//关注变量
                    //yanChi();
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30011, 1);//关注变量
                    //yanChi();
                    WZ_Link_Inter.Instance.Set_AATT_Var(Sn, 30013, 1);//关注变量
                    int status = WZ_Link_Inter.Instance.Get_Dev_Online(Sn, 30008);//判断设备是否在线？
                    if (status==1)
                    {
                        lianJie.Content = "在线！。。";
                    }
                    else
                    {
                        lianJie.Content = "离线状态！。。";
                    }
                    WZ_Link_Inter.Instance.Get_var_Pranm(Sn, 30008, 1);//主动查询变量的值。
                }
            }));
        }

        //接收到数据时
        private void VarChanged(uint arg1, Waver_Inter.Entity.VarEntity arg2)
        {
            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                
                //温湿度
                if (arg2.Uid==30008)
                {
                    Console.WriteLine("uid:" + arg2.Uid + "/id:" + arg2.ID + "/index:" + arg2.VarIndex + "/devid:" + arg2.DevID + "/值:" + arg2.VarValue);
                    if (arg2.VarIndex==1)
                    {
                        wenDu.Content = "温度：" + arg2.VarValue;
                    }
                    if (arg2.VarIndex== 2)
                    {
                        shiDu.Content = "湿度：" + arg2.VarValue;
                    }
                }
                //光敏
                if (arg2.Uid==30009)
                {
                    guangMing.Content = "光敏：" + arg2.VarValue;
                }
                //可燃气体
                if (arg2.Uid==30010)
                {
                    qiTi.Content = "可燃气体:" + arg2.VarValue;
                }
                //人体红外
                if (arg2.Uid==30011)
                {
                    hongWai.Content = "人体红外:" + arg2.VarValue;
                }
            }));
        }
        //RFID卡返回数据
        private void StrChanged(uint sn, StrEntity strEntity)
        {
            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                if (strEntity == null) return;
                RFIDTEXT.Content = "RFID：" + strEntity.StrBuf;
            }));
        }
        //订阅设备变化时
        private void DevChanged(uint arg1, Waver_Inter.Entity.DeviceEntity arg2)
        {
            App.Current.Dispatcher.Invoke((Action)(() =>
            {


            }));
        }
        private void KaiShi_Click(object sender, RoutedEventArgs e)
        {
            //string str = "";
            //lianJie.Content = dianZhengText();
            //直接登陆网关。
            try
            {
                lianJie.Content = "正在登陆";
                WZ_Link_Inter.Instance.Login_Host(Sn, "192.168.31.106");//网关ip地址
            }
            catch (Exception ex)
            {

            }
        }
        //返回点阵的选择内容
        public string dianZhengText()
        {
            string str = "0";
            if (dianZheng0.IsChecked==true)
            {
                str = dianZheng0.Content.ToString();
            }
            if (dianZheng1.IsChecked == true)
            {
                str = dianZheng1.Content.ToString();
            }
            if (dianZheng2.IsChecked == true)
            {
                str = dianZheng2.Content.ToString();
            }
            return str;
        }
        //返回RGB选择内容
        public string rgbText()
        {
            string str = "0";
            if (RGB0.IsChecked==true)
            {
                str = RGB0.Content.ToString();
            }
            if (RGB1.IsChecked == true)
            {
                str = RGB1.Content.ToString();
            }
            if (RGB2.IsChecked == true)
            {
                str = RGB2.Content.ToString();
            }
            return str;
        }
        //语音播报text
        public string yuYinText()
        {
            string str = "0";
            if (yuYin0.IsChecked==true)
            {
                str = yuYin0.Content.ToString();
            }
            if (yuYin1.IsChecked == true)
            {
                str = yuYin1.Content.ToString();
            }
            if (yuYin2.IsChecked == true)
            {
                str = yuYin2.Content.ToString();
            }
            return str;
        }
        //延迟1000毫秒
        public void yanChi()
        {
            Thread.Sleep(1000);
        }
        //风扇开
        private void FenShanKai_Click(object sender, RoutedEventArgs e)
        {
            WZ_Link_Inter.Instance.Set_Var_Param(Sn, 30016, 1, 1);
        }
        //风扇关
        private void FenShanGuan_Click(object sender, RoutedEventArgs e)
        {
            WZ_Link_Inter.Instance.Set_Var_Param(Sn, 30016, 1, 0);

        }
        //点阵发送
        private void DianZhenFaSong_Click(object sender, RoutedEventArgs e)
        {
            string si = dianZhengText();
            
            WZ_Link_Inter.Instance.Set_Var_Param(Sn, 30021, 1, Convert.ToUInt16(si));
        }
        //RGB发送
        private void RGBFaSong_Click(object sender, RoutedEventArgs e)
        {
            string si = rgbText();
            WZ_Link_Inter.Instance.Set_Var_Param(Sn, 30018, 1, Convert.ToUInt16(si));
        }
        //发送语音
        private void YuYinFaSong_Click(object sender, RoutedEventArgs e)
        {
            string si = yuYinText();
            WZ_Link_Inter.Instance.Set_Var_Param(Sn, 30023, 1, Convert.ToUInt16(si));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            KeHuDuan kehu = new KeHuDuan();  //跳转页面
            kehu.Show();
        }
    }
}
